package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://bbs.sina.com.cn/ 新浪论坛
 * @author allen
 */
public class SinaBbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://bbs..*.sina.com.cn/thread-.*-1.html",
				"http://.*.bbs..*.sina.com.cn/thread-.*.html",
				"http://forum..*.sina.com.cn/.*thread-.*-1-1.html",
				"http://.*.bbs.house.sina.com.cn/thread-[0-9]*-1.html",
				"http://forum..*.sina.com.cn/.*viewthread.php\\?tid=[0-9]+&extra=.*&page=1",
				"http://.*bbs..*.sina.com.cn/.*thread-.*-1-1.html",
				"http://club.*.sina.com.cn/.*thread-.*-1-1.html",
				"http://club.*.sina.com.cn/viewthread.php\\?tid=[0-9]*.*",
				"http://club..*.sina.com.cn/.*thread-.*-1-1.html",
				"http://.*.bbs.house.sina.com.cn/bbs/post/show\\?pid=[0-9]*",
				"http://.*.bbs.house.sina.com.cn/[0-9]*/thread.html",
				"http://club.*.sina.com.cn/slide.php\\?.*tid=[0-9]*",
				"http://guba.sina.com.cn/\\?bid=[0-9]*&s=thread&tid=[0-9]*"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		String author = null;
		Long date = null;
		Integer clickNum=0;
		Integer commentNum=0;

		Document doc = Jsoup.parse(htmltxt);
		title = doc.select("span#thread_subject").text();
		if(StringUtils.isBlank(title)){
			title = doc.select("h4.ilt_tit").text();
		}
		if(StringUtils.isBlank(title)){
			title = doc.select(".viewthread h1").text();
		}
		if (StringUtils.isBlank(title)){
			title =doc.select("title").text();
			if(title.indexOf("_")!=-1){
				title=title.substring(0,title.indexOf("_"));
			}
		}

			Elements select = doc.select("html body div.wrap div.maincont div.main_l div");
			if(select.size()>0){
				content = HtmlCleaner.getContentHtml(url, select.first());
				String tempdate=select.get(0).select("div.mybbs div.myInfo div.myInfo_up font").text();
				if(tempdate==null || tempdate.trim().length()==0){
				}
				date = DateUtils.matchDate(tempdate);

				String clickComentstr=select.get(0).select("div.mybbs_cont").text();
				clickNum=NumberUtils.matchNumber(StringUtils.substringBetween(clickComentstr, "该帖被浏览", "次"));
				commentNum=NumberUtils.matchNumber(StringUtils.substringBetween(clickComentstr, "回复", "次"));
			}else{
				select = doc.select("html body div.wrap form div.mainbox");
				if(select.size()>0){
					content = HtmlCleaner.getContentHtml(url,select.first());
					String tempdate=select.get(0).select("div.postinfo").text();
					date = DateUtils.matchDate(tempdate);

					String clickComentstr=select.get(0).select("div.postmessage").text();
					clickNum=NumberUtils.matchNumber(StringUtils.substringBetween(clickComentstr, "该帖被浏览", "次"));
					commentNum=NumberUtils.matchNumber(StringUtils.substringBetween(clickComentstr, "回复", "次"));
				}else{
					select = doc.select("html body div#wrap.wrap div#dfviewthread div#dfview div.mainbox");
					if(select.size()>0){
						content = HtmlCleaner.getContentHtml(url, select.get(0).select("div.t_msgfont"));
						String tempdate=select.get(0).select("div.postinfo").text();
						if(tempdate==null || tempdate.trim().length()==0){
						}
						date = DateUtils.matchDate(tempdate);

						String clickComentstr=doc.select("div#dfviewthread div#dfview div.mainbox_title span.headactions").text();
						clickNum=NumberUtils.matchNumber(StringUtils.substringAfter(clickComentstr, "阅读:"));
						commentNum=NumberUtils.matchNumber(StringUtils.substringAfter(clickComentstr, "回复"));
					}else{
						date = DateUtils.matchDate(doc.select("tr td.postcontent div.postinfo").text());
						if(date==null){
							date = DateUtils.matchDate(doc.select("html body div#wrap.wrap div.mainbox ins").text());
						}
						content = HtmlCleaner.getContentHtml(url, doc.select("div.postmessage"));
					}
				}
			}
			if(StringUtils.isBlank(content)){

				content = HtmlCleaner.getContentHtml(url, doc.select("td.t_f"));
			}
			if(StringUtils.isBlank(content)){
				content = HtmlCleaner.getContentHtml(url,  doc.select("div.ilt_p"));
			}

			if(StringUtils.isBlank(title)){
				title = doc.select("title").text();
			}
			if (StringUtils.isBlank(author)){
				if (doc.select("a.xw1").size() > 0) {
					author = doc.select("a.xw1").get(0).text();
				}
			}
			if (date==null){
				date = DateUtils.matchDate(doc.select("div.authi").text());
			}
			if(title.endsWith("-新浪乐居论坛")){
				title=StringUtils.substringBeforeLast(title, "-新浪乐居论坛");
			}
			bbs.setTitle(StringUtils.trimSpace(title));
			if(StringUtils.isBlank(content)){
				content = HtmlCleaner.getContentHtml(url, doc.select("div.pcon_con").first());
			}
			if(StringUtils.isBlank(content)){
				Elements select2 = doc.select("div#eData dl");
				if( select2.size()!=0){
					select2=select2.select("dd");
					if(select2.size()>4){
						content = HtmlCleaner.getContentHtml(url, select2.get(4));
					}
				}
			}

			if (content!=null && content.contains("论坛精选：欢迎进入")){
				content = StringUtils.substringBefore(content, "论坛精选：欢迎进入");
			}
			bbs.setContent(content);

			if(date==null){
				date = DateUtils.matchDate(doc.select("div.postmessage i").text());
			}
			if(date==null){
				date = DateUtils.matchDate(doc.select("div#floor_thread.mainbox table tbody tr td.postauthor dl").text());
			}
			if(date==null){
				if(doc.select("div.mt5").size()>0){
					date = DateUtils.matchDate(doc.select("div.mt5").get(0).text());
				}
			}

			if(date==null){
				date = DateUtils.matchDate(doc.select("div#eData dd").html());
			}
			if(date==null){
				date = DateUtils.matchDate(doc.select("div.iltp_time").text());
			}
			if((clickNum==null && commentNum==null) || (clickNum==0&&commentNum==0)){
				String text = doc.select("div.ydtitle").text();
				clickNum=NumberUtils.matchNumber(StringUtils.substringBefore(text, "阅读"));
				commentNum=NumberUtils.matchNumber(StringUtils.substringAfter(text, "阅读"));
			}

			if((clickNum==null && commentNum==null) || (clickNum==0&&commentNum==0)){
				String text = doc.select("div.iltp_span").text();
				clickNum=NumberUtils.matchNumber(StringUtils.substringBetween(text, "阅读数",")"));
				commentNum=NumberUtils.matchNumber(StringUtils.substringAfterLast(text, "|"));
			}

			Elements select2 = doc.select("div.postscon div.fl div.ml20 a");
			if(select2!=null && select2.size()!=0){
				author=select2.get(0).text();
			}
			if(StringUtils.isBlank(author)){
				select2=doc.select("div.myInfo_up a");
				if(select2.size()>0){
					author=select2.get(0).text();
				}
			}

			if(StringUtils.isBlank(author)){
				select2=doc.select("td.postauthor span.dropmenu");
				if(select2!=null && select2.size()!=0){
					select2=select2.get(0).select("a");
					if(select2!=null && select2.size()!=0){
						author=select2.get(0).html();
					}
				}
			}

			if(StringUtils.isBlank(author)){
				select2=doc.select("html body#viewthread div#wrap.wrap div#postlist.mainbox div table tbody tr td.postauthor div.postinfo a");
				if(select2.size()>0){
					author=select2.get(0).html();
				}
			}
			if(StringUtils.isBlank(author)){
				Element first = doc.select("span.ilt_name").first();
				if(first!=null) author=first.text();
			}
			bbs.setAuthor(author);
			bbs.setDate(date);
			bbs.setClickNum(clickNum);
			bbs.setCommentNum(commentNum);
		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String html = responseToURL.getHtml();
					if(html!=null){
						Integer clickNum=0;
						Integer commentNum=0;
						Document doc = Jsoup.parse(html);
						Elements select = doc.select("html body div.wrap div.maincont div.main_l div");
						if(select.size()>0){
							String clickComentstr=select.get(0).select("div.mybbs_cont").text();
							if(clickComentstr!=null && clickComentstr.trim().length()!=0){
								if(clickComentstr.indexOf("该帖被浏览")!=-1){
									String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("该帖被浏览")+"该帖被浏览".length());

									if(clickNumstr.indexOf("次")!=-1){
										clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf("次"));
										clickNumstr=clickNumstr.trim();
										clickNumstr=clickNumstr.replaceAll(" ", "");
										clickNumstr=clickNumstr.replaceAll(" ", "");
										clickNumstr=clickNumstr.replaceAll(",", "");
										try {
											clickNum=Integer.parseInt(clickNumstr);
										} catch (Exception e) {
											// TODO: handle exception
										}
									}
								}
								if(clickComentstr.indexOf("回复")!=-1){
									String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("回复")+"回复".length());
									if(commentNumstr.indexOf("次")!=-1){
											commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf("次"));
										commentNumstr=commentNumstr.trim();
										commentNumstr=commentNumstr.replaceAll(" ", "");
										commentNumstr=commentNumstr.replaceAll(" ", "");
										commentNumstr=commentNumstr.replaceAll(",", "");
										try {
											commentNum=Integer.parseInt(commentNumstr);
										} catch (Exception e) {
											// TODO: handle exception
										}
									}
								}
							}
						}else{
							select = doc.select("html body div.wrap form div.mainbox");
							if(select.size()>0){
								String clickComentstr=select.get(0).select("div.postmessage").text();

								if(clickComentstr!=null && clickComentstr.trim().length()!=0){
									if(clickComentstr.indexOf("该帖被浏览")!=-1){
										String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("该帖被浏览")+"该帖被浏览".length());
										if(clickNumstr.indexOf("次")!=-1){
											clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf("次"));
											clickNumstr=clickNumstr.trim();
											clickNumstr=clickNumstr.replaceAll(" ", "");
											clickNumstr=clickNumstr.replaceAll(" ", "");
											clickNumstr=clickNumstr.replaceAll(",", "");
											try {
												clickNum=Integer.parseInt(clickNumstr);
											} catch (Exception e) {
												// TODO: handle exception
											}
										}
									}
									if(clickComentstr.indexOf("回复")!=-1){
										String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("回复")+"回复".length());
										if(commentNumstr.indexOf("次")!=-1){
												commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf("次"));
											commentNumstr=commentNumstr.trim();
											commentNumstr=commentNumstr.replaceAll(" ", "");
											commentNumstr=commentNumstr.replaceAll(" ", "");
											commentNumstr=commentNumstr.replaceAll(",", "");
											try {
												commentNum=Integer.parseInt(commentNumstr);
											} catch (Exception e) {
												// TODO: handle exception
											}
										}
									}
								}
							}else{
								select = doc.select("html body div#wrap.wrap div#dfviewthread div#dfview div.mainbox");
								if(select.size()>0){
									String clickComentstr=doc.select("div#dfviewthread div#dfview div.mainbox_title span.headactions").text();
									if(clickComentstr!=null && clickComentstr.trim().length()!=0){
										if(clickComentstr.indexOf("阅读:")!=-1){
											String clickNumstr=clickComentstr.substring(clickComentstr.indexOf("阅读:")+"阅读:".length());
											if(clickNumstr.indexOf(" ")!=-1){
												clickNumstr=clickNumstr.substring(0,clickNumstr.indexOf(" "));
												clickNumstr=clickNumstr.trim();
												clickNumstr=clickNumstr.replaceAll(" ", "");
												clickNumstr=clickNumstr.replaceAll(" ", "");
												clickNumstr=clickNumstr.replaceAll(",", "");
												try {
													clickNum=Integer.parseInt(clickNumstr);
												} catch (Exception e) {
													// TODO: handle exception
												}
											}
										}
										if(clickComentstr.indexOf("回复:")!=-1){
											String commentNumstr=clickComentstr.substring(clickComentstr.indexOf("回复:")+"回复:".length());
											if(commentNumstr.indexOf("）")!=-1){
													commentNumstr=commentNumstr.substring(0,commentNumstr.indexOf("）"));
												commentNumstr=commentNumstr.trim();
												commentNumstr=commentNumstr.replaceAll(" ", "");
												commentNumstr=commentNumstr.replaceAll(" ", "");
												commentNumstr=commentNumstr.replaceAll(",", "");
												try {
													commentNum=Integer.parseInt(commentNumstr);
												} catch (Exception e) {
													// TODO: handle exception
												}
											}
										}
									}
								}
							}
						}
						meta.setClickNum(clickNum);
						meta.setCommentNum(commentNum);
						return meta;
					}

				}
			}
		}
		return null;
	}
    
	
}
